package org.peterbjornx.graphics.jogl;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * This class handles the key events. It extends the KeyAdapter Class.
 * <p>
 *
 * @version $Id: KeyHandler.java 813 2008-06-03 16:31:35Z mzaczek $
 * @since CTS 1.0
 */
public class KeyHandler extends KeyAdapter {

  /** The myrenderer object on which key events are to act */

 
  /** Flag used for tracking ctrl-<key> combos */
  private boolean _ctrlActive = false;
  
  /** Flag tracking control mode */
  private boolean vehicleControl = false;
    public AbstractJOGLRenderer myrenderer;


    /**
   * Checks if a key is typed.  This method currently does not respond to
   * this event
   *
   * @param e A key typed event
   */
  public void keyTyped(KeyEvent e) {
    // Nothing
  }

  /**
   * Checks if a key is pressed and calls the appropriate method to process
   * the event.
   *
   * @param e A key pressed event
   */
  public void keyPressed(KeyEvent e) {
    processKeyEvent(e, true);
  }

  /**
   * Checks if a key is released.  This method currently does not respond to
   * this event
   *
   * @param e A key released event
   */
  public void keyReleased(KeyEvent e) {
    // Nothing
  }

  /**
   * Process the key events that are sensed.
   *
   * @param e A key event
   * @param pressed Flag signifying whether the key has been pressed or not
   */
  private void processKeyEvent(KeyEvent e, boolean pressed) {

    switch (e.getKeyCode()) {


      case KeyEvent.VK_UP:
      case KeyEvent.VK_DOWN:
      case KeyEvent.VK_LEFT:
      case KeyEvent.VK_RIGHT:
      case KeyEvent.VK_PAGE_UP:
      case KeyEvent.VK_PAGE_DOWN:


        Vector3d tmpLook = new Vector3d(myrenderer.vLook);
        Vector3d tmpRight = new Vector3d(myrenderer.vRight);

        double moveSpeed = 10.00;

        if (e.getKeyCode() == KeyEvent.VK_UP) {
          myrenderer.vEye.x -= tmpLook.x * -moveSpeed;
          myrenderer.vEye.y -= tmpLook.y * -moveSpeed;
          myrenderer.vEye.z -= tmpLook.z * -moveSpeed;
        }

        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
          myrenderer.vEye.x += tmpLook.x * -moveSpeed;
          myrenderer.vEye.y += tmpLook.y * -moveSpeed;
          myrenderer.vEye.z += tmpLook.z * -moveSpeed;
        }

        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
          myrenderer.vEye.x -= tmpRight.x * moveSpeed;
          myrenderer.vEye.y -= tmpRight.y * moveSpeed;
          myrenderer.vEye.z -= tmpRight.z * moveSpeed;
        }

        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
          myrenderer.vEye.x += tmpRight.x * moveSpeed;
          myrenderer.vEye.y += tmpRight.y * moveSpeed;
          myrenderer.vEye.z += tmpRight.z * moveSpeed;
        }

        if (e.getKeyCode() == KeyEvent.VK_PAGE_UP) {
          myrenderer.vEye.y += moveSpeed;
        }
        if (e.getKeyCode() == KeyEvent.VK_PAGE_DOWN) {
          myrenderer.vEye.y -= moveSpeed;
        }

        break;

      case KeyEvent.VK_COMMA:
        if (!myrenderer.emulateFPS) {
          myrenderer.rotateLookVector(-1.0);
        }
        break;
      case KeyEvent.VK_PERIOD:
        if (!myrenderer.emulateFPS) {
          myrenderer.rotateLookVector(1.0);
        }
        break;
        
      case KeyEvent.VK_F:
        myrenderer.emulateFPS = !myrenderer.emulateFPS;
        break;
      case KeyEvent.VK_T:
        myrenderer.wireframe = !myrenderer.wireframe;
        break;
      case KeyEvent.VK_C:
        myrenderer.textured =!myrenderer.textured;
      case KeyEvent.VK_D:
        myrenderer.depthtest =!myrenderer.depthtest;
        break;
      default:
    }

    // Update the scene - redraw the display following a keyboard event
   // thePanel.drawabledisplay();
  }
}